/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.gui;

import java.awt.Frame;
import java.math.BigDecimal;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import program.beans.BillAmount;

/**
 *
 * Responsible: Patrick, Bernhard
 */
public class NewAmountDlg extends javax.swing.JDialog {
    
    private boolean ok;
    private ButtonGroup buttonGroup = new ButtonGroup();
    private BillAmount ba;

    /**
     * Creates new form NewAmountDlg
     */
    public NewAmountDlg(JDialog parent, boolean modal, BillAmount ba) {
        super(parent, modal);
        initComponents();
        buttonGroup.add(rb0);
        buttonGroup.add(rb10);
        buttonGroup.add(rb12);
        buttonGroup.add(rb20);
        buttonGroup.add(rbOther);
        buttonGroup.setSelected(rb10.getModel(), true);
        this.setLocationRelativeTo(parent);
        
        if(ba != null)
        {
            tfBrutto.setText(ba.getBrutto()+"");
            double perc = ba.getPercentage().doubleValue();
            if(perc == 10)
                buttonGroup.setSelected(rb10.getModel(), true);
            else if(perc == 12)
                buttonGroup.setSelected(rb12.getModel(), true);
            else if(perc == 20)
                buttonGroup.setSelected(rb20.getModel(), true);
            else if(perc == 0)
                buttonGroup.setSelected(rb0.getModel(), true);
            else{
                buttonGroup.setSelected(rbOther.getModel(), true);
                tfOtherPerc.setText(""+ ba.getPercentage());
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfBrutto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        rb12 = new javax.swing.JRadioButton();
        rb10 = new javax.swing.JRadioButton();
        rb20 = new javax.swing.JRadioButton();
        tfOtherPerc = new javax.swing.JTextField();
        rbOther = new javax.swing.JRadioButton();
        rb0 = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Neuer Betrag");
        setResizable(false);

        jLabel1.setText("Bruttobetrag:");

        tfBrutto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                onEnter(evt);
            }
        });

        jLabel2.setText("Steuersatz:");

        rb12.setText("12 %");
        rb12.setActionCommand("12");

        rb10.setText("10 %");
        rb10.setActionCommand("10");

        rb20.setText("20 %");
        rb20.setActionCommand("20");

        rbOther.setText("anderer Betrag:");
        rbOther.setActionCommand("other");

        rb0.setText("0 %");
        rb0.setActionCommand("0");

        jLabel3.setText("(Wird nicht für die Umsatzvorsteueranmeldung verwendet!)");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfBrutto, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rb12)
                            .addComponent(rb20)
                            .addComponent(rb10)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(rb0)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(rbOther)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfOtherPerc, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(tfBrutto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rb0)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rb10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rb12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rb20, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbOther))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addComponent(tfOtherPerc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOk(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 20, 10, 20);
        jPanel2.add(jButton1, gridBagConstraints);

        jButton2.setText("Abbrechen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCancel(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 20, 10, 20);
        jPanel2.add(jButton2, gridBagConstraints);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onOk(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onOk
        
        String bruttoStr = tfBrutto.getText().replaceAll(",", "\\.");
        if(bruttoStr.length() > 0)
        {
            try {
                BigDecimal brutto = new BigDecimal(bruttoStr);
                BigDecimal percentage;
                String percentageStr = buttonGroup.getSelection().getActionCommand();
                if(percentageStr.equals("other"))
                {
                    
                    percentageStr = tfOtherPerc.getText();
                    if(Double.parseDouble(percentageStr) >= 0){
                        
                        percentageStr = percentageStr.replaceAll(",", "\\.");
                    }
                    else
                        throw new Exception("");
                    
                    
                }
                
                percentage = new BigDecimal(percentageStr);
                ba = new BillAmount(brutto, percentage);
                ok = true;
                this.setVisible(false);
                
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Sie müssen gültige Zahlen eingeben!", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }else
        {
            JOptionPane.showMessageDialog(this, "Sie müssen keinen Bruttobetrag eingeben!", "Error", JOptionPane.ERROR_MESSAGE);
        }
        
        
    }//GEN-LAST:event_onOk

    private void onCancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onCancel
        ok = false;
        this.setVisible(false);
    }//GEN-LAST:event_onCancel

    private void onEnter(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onEnter
        if(evt.getKeyCode() == 10)
            onOk(null);
    }//GEN-LAST:event_onEnter

    public boolean isOk()
    {
        return ok;
    }
    
    public BillAmount getBillAmount()
    {
        return ba;
    }
    
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton rb0;
    private javax.swing.JRadioButton rb10;
    private javax.swing.JRadioButton rb12;
    private javax.swing.JRadioButton rb20;
    private javax.swing.JRadioButton rbOther;
    private javax.swing.JTextField tfBrutto;
    private javax.swing.JTextField tfOtherPerc;
    // End of variables declaration//GEN-END:variables
}
